package com.ruoyi.server.api.subscribe.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.errorCode.CommonException;
import com.ruoyi.common.exception.errorCode.EmErrorCode;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.validator.ValidatorUtil;
import com.ruoyi.server.api.coach.domain.CoachManagement;
import com.ruoyi.server.api.subscribe.model.CourseSubscribeModel;
import io.lettuce.core.RedisURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.server.api.subscribe.mapper.CourseSubscribeMapper;
import com.ruoyi.server.api.subscribe.domain.CourseSubscribe;
import com.ruoyi.server.api.subscribe.service.ICourseSubscribeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 团课信息Service业务层处理
 *
 * @author hjh
 * @date 2024-06-10
 */
@Service
public class CourseSubscribeServiceImpl implements ICourseSubscribeService {
    @Autowired
    private CourseSubscribeMapper courseSubscribeMapper;

    /**
     * 查询团课信息
     *
     * @param id 团课信息主键
     * @return 团课信息
     */
    @Override
    public CourseSubscribe selectCourseSubscribeById(Long id) {
        return courseSubscribeMapper.selectCourseSubscribeById(id);
    }

    /**
     * 查询团课信息列表
     *
     * @param courseSubscribe 团课信息
     * @return 团课信息
     */
    @Override
    public List<CourseSubscribe> selectCourseSubscribeList(CourseSubscribe courseSubscribe) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
        //判断场馆标识是否为空
        if (StringUtils.isNotEmpty(venueUniqueIdentifier)) {
            courseSubscribe.setVenueUniqueIdentifier(venueUniqueIdentifier);
            List<CourseSubscribe> courseSubscribes = courseSubscribeMapper.selectCourseSubscribeList(courseSubscribe);
            return courseSubscribes;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 新增团课信息
     *
     * @param courseSubscribeModel 团课信息
     * @return 结果
     */
    @Override
    public AjaxResult insertCourseSubscribe(CourseSubscribeModel courseSubscribeModel) throws Exception {
        if (StringUtils.isNotEmpty(courseSubscribeModel.getCourseName())) {
            CourseSubscribe courseSubscribe = new CourseSubscribe();
            BeanUtils.copy(courseSubscribeModel, courseSubscribe);
            StringBuilder stringBuilder = new StringBuilder();
            if (StringUtils.isNotEmpty(courseSubscribeModel.getCoverPic())) {
                stringBuilder.append(courseSubscribeModel.getCoverPic());
            }
            //添加图片数组
            if (stringBuilder.length() > 0) {
                courseSubscribe.setCoverPic(stringBuilder.toString());
            } else {
                courseSubscribe.setCoverPic("");
            }
            courseSubscribe.setCourseCreateTime(new Date());
            SysUser user = SecurityUtils.getLoginUser().getUser();
            String venueUniqueIdentifier = user.getVenueUniqueIdentifier();
            //添加租户标识
            if (StringUtils.isNotEmpty(venueUniqueIdentifier)) {
                courseSubscribe.setVenueUniqueIdentifier(venueUniqueIdentifier);
            } else {
                return AjaxResult.success(EmErrorCode.SAVE_ERROR);
            }
            //免费情况（处理金额，支付方式默认为到付,支付金额为0）
            if (courseSubscribe.getCollectFee() == 1){
                courseSubscribe.setPayWay(1);
                courseSubscribe.setCollectFeeMoney(0L);
            }
            int insert = courseSubscribeMapper.insertCourseSubscribe(courseSubscribe);
            if (insert > 0) {
                return AjaxResult.success(insert);
            } else {
                return AjaxResult.success(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return AjaxResult.error("团课名称不能为空");
        }

    }


    /**
     * @param courseSubscribeModel
     * @Author hjh
     * @Description 修改团课信息
     * @Date 2024/6/11 14:19
     * @Return int
     */
    @Override
    public AjaxResult updateCourseSubscribe(CourseSubscribeModel courseSubscribeModel) throws Exception {
        if (ValidatorUtil.ValidationObject(courseSubscribeModel)) {
            StringBuilder stringBuilder = new StringBuilder();
            if (StringUtils.isNotEmpty(courseSubscribeModel.getCoverPic())) {
                stringBuilder.append(courseSubscribeModel.getCoverPic());
            }
            CourseSubscribe courseSubscribe = new CourseSubscribe();
            LoginUser loginUser = SecurityUtils.getLoginUser();
            BeanUtils.copy(courseSubscribeModel, courseSubscribe);
            String venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
            courseSubscribe.setVenueUniqueIdentifier(venueUniqueIdentifier);
            //添加图片数组
            if (stringBuilder.length() > 0) {
                courseSubscribe.setCoverPic(stringBuilder.toString());
            } else {
                courseSubscribe.setCoverPic("");
            }
            int i = courseSubscribeMapper.updateCourseSubscribe(courseSubscribe);
            return AjaxResult.success(i);
        } else {
            return AjaxResult.success(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
     * 批量删除团课信息
     *
     * @param ids 需要删除的团课信息主键
     * @return 结果
     */
    @Override
    public int deleteCourseSubscribeByIds(Long[] ids) {
        return courseSubscribeMapper.deleteCourseSubscribeByIds(ids);
    }

    /**
     * 删除团课信息信息
     *
     * @param id 团课信息主键
     * @return 结果
     */
    @Override
    public int deleteCourseSubscribeById(Long id) {
        return courseSubscribeMapper.deleteCourseSubscribeById(id);
    }

    /**
     * @param id
     * @Author hjh
     * @Description 修改团课状态
     * @Date 2024/6/11 14:12
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult updateCourseState(Integer id) {
        if (ObjectUtils.isNotEmpty(id)) {
            int i = courseSubscribeMapper.updateCourseState(id);
            if (i > 0) {
                return AjaxResult.success(i);
            }
        } else {
            return AjaxResult.success(EmErrorCode.UPDATE_ERROR);
        }
        return AjaxResult.success(EmErrorCode.UPDATE_ERROR);
    }

    /**
     * @param courseSubscribe
     * @Author hjh
     * @Description 查询团课信息(已发布)
     * @Date 2024/6/12 8:45
     * @Return java.util.List<com.ruoyi.server.api.subscribe.domain.CourseSubscribe>
     */
    @Override
    public List<CourseSubscribe> getReleaseCourse(CourseSubscribe courseSubscribe) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
        //判断场馆标识是否为空
        if (StringUtils.isNotEmpty(venueUniqueIdentifier)) {
            courseSubscribe.setVenueUniqueIdentifier(venueUniqueIdentifier);
            List<CourseSubscribe> courseSubscribes = courseSubscribeMapper.getReleaseCourse(courseSubscribe);
            return courseSubscribes;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * @param courseSubscribe
     * @Author hjh
     * @Description 查询团课信息(过往团课)
     * @Date 2024/6/12 8:57
     * @Return java.util.List<com.ruoyi.server.api.subscribe.domain.CourseSubscribe>
     */
    @Override
    public List<CourseSubscribe> getAbnormalCourse(CourseSubscribe courseSubscribe) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
        //判断场馆标识是否为空
        if (StringUtils.isNotEmpty(venueUniqueIdentifier)) {
            courseSubscribe.setVenueUniqueIdentifier(venueUniqueIdentifier);
            List<CourseSubscribe> courseSubscribes = courseSubscribeMapper.getAbnormalCourse(courseSubscribe);
            return courseSubscribes;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * @param id
     * @param cancelCause
     * @Author hjh
     * @Description 撤销团课
     * @Date 2024/6/12 15:16
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult updateCancelRelease(Integer id, String cancelCause) {
        if (ObjectUtils.isNotEmpty(id) && StringUtils.isNotEmpty(cancelCause)) {
            int i = courseSubscribeMapper.updateCancelRelease(id, cancelCause);
            if (i > 0) {
                return AjaxResult.success(i);
            } else {
                return AjaxResult.success(EmErrorCode.SAVE_ERROR);
            }
        }
        return AjaxResult.success(EmErrorCode.SAVE_ERROR);
    }

    @Override
    public AjaxResult getVenueUniqueIdentifier() {
        String venueUniqueIdentifier = SecurityUtils.getLoginUser().getUser().getVenueUniqueIdentifier();
        return AjaxResult.success(venueUniqueIdentifier);
    }

    /**
    * @Author hjh
    * @Description 获取教练信息
    * @Date 2024/6/14 10:31
    * @Return com.ruoyi.common.core.domain.AjaxResult

    */
    @Override
    public AjaxResult getCouchInfo() {
        String venueUniqueIdentifier = SecurityUtils.getLoginUser().getUser().getVenueUniqueIdentifier();
        List<CoachManagement> couchInfo = courseSubscribeMapper.getCouchInfo(venueUniqueIdentifier);
        return AjaxResult.success(couchInfo);
    }
}
